if(window.addEventListener)
{
	window.addEventListener('load', function() {
	    setTimeout(scrollTo, 0, 0, 1);
	}, false);
}
else	
{
	window.attachEvent('load', function() {
	    setTimeout(scrollTo, 0, 0, 1);
	});
}


var map;
var chicago = new google.maps.LatLng(41.850033, -87.6500523);
var marker= [];
var myMarker;
var accountControl;
var infowindow;

/**
 * The HomeControl adds a control to the map that simply
 * returns the user to Chicago. This constructor takes
 * the control DIV as an argument.
 */

function CallToControl(controlDiv, map) {
  // Set CSS styles for the DIV containing the control
  // Setting padding to 5 px will offset the control
  // from the edge of the map
  controlDiv.style.padding = '5px';

  // Set CSS for the control border
  var controlUI = document.createElement('DIV');
  controlUI.style.backgroundColor = 'white';
  controlUI.style.borderStyle = 'solid';
  controlUI.style.borderWidth = '2px';
  controlUI.style.cursor = 'pointer';
  controlUI.style.textAlign = 'center';
  controlUI.title = '';
  controlDiv.appendChild(controlUI);

  // Set CSS for the control interior
  var controlText = document.createElement('DIV');
  controlText.style.fontFamily = 'Arial,sans-serif';
  controlText.style.fontSize = '12px';
  controlText.style.paddingLeft = '4px';
  controlText.style.paddingRight = '4px';
  controlText.innerHTML = '이곳으로';
  controlUI.appendChild(controlText);

  // Setup the click event listeners: simply set the map to
  // Chicago
  google.maps.event.addDomListener(controlUI, 'click', function() {
    //map.setCenter(chicago)
	    //requestHello('/icg_msh_test2');
	  myMarker.setPosition(map.getCenter());
  });
}

function MoveToControl(controlDiv, map) {
	  // Set CSS styles for the DIV containing the control
	  // Setting padding to 5 px will offset the control
	  // from the edge of the map
	  controlDiv.style.padding = '5px';

	  // Set CSS for the control border
	  var controlUI = document.createElement('DIV');
	  controlUI.style.backgroundColor = 'white';
	  controlUI.style.borderStyle = 'solid';
	  controlUI.style.borderWidth = '2px';
	  controlUI.style.cursor = 'pointer';
	  controlUI.style.textAlign = 'center';
	  controlUI.title = '';
	  controlDiv.appendChild(controlUI);

	  // Set CSS for the control interior
	  var controlText = document.createElement('DIV');
	  controlText.style.fontFamily = 'Arial,sans-serif';
	  controlText.style.fontSize = '12px';
	  controlText.style.paddingLeft = '4px';
	  controlText.style.paddingRight = '4px';
	  controlText.innerHTML = '내위치로';
	  controlUI.appendChild(controlText);

	  // Setup the click event listeners: simply set the map to
	  // Chicago
	  google.maps.event.addDomListener(controlUI, 'click', function() {
	    //map.setCenter(chicago)
		    //requestHello('/icg_msh_test2');
		    map.panTo(myMarker.getPosition());
	  });
	}

function AccountControl(controlDiv, map) {
	  // Set CSS styles for the DIV containing the control
	  // Setting padding to 5 px will offset the control
	  // from the edge of the map
	  controlDiv.style.padding = '5px';

	  // Set CSS for the control border
	  var controlUI = document.createElement('DIV');
	  controlUI.style.backgroundColor = 'white';
	  controlUI.style.borderStyle = 'solid';
	  controlUI.style.borderWidth = '2px';
	  controlUI.style.cursor = 'pointer';
	  controlUI.style.textAlign = 'center';
	  controlUI.title = '';
	  controlDiv.appendChild(controlUI);

	  // Set CSS for the control interior
	  var controlText = document.createElement('DIV');
	  controlText.style.fontFamily = 'Arial,sans-serif';
	  controlText.style.fontSize = '12px';
	  controlText.style.paddingLeft = '4px';
	  controlText.style.paddingRight = '4px';
	  controlText.innerHTML = sign;
	  controlUI.appendChild(controlText);

	  // Setup the click event listeners: simply set the map to
	  // Chicago
	  google.maps.event.addDomListener(controlUI, 'click', function() {
	    //map.setCenter(chicago)
		    //requestHello('/icg_msh_test2');
		  if(myMarker)
		    map.panTo(myMarker.getPosition());
	  });
	}



var xhr = null;
function getXMLHttpRequest() {	
    if (window.ActiveXObject) {
        try {
        	alert("logout");
        	//내마커 삭제
            myMarker.setMap(null);
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e1) {
            try {
                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {
                return null;
            }
        }
    } else if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else {
        return null;
    }
}

function requestHello(URL) {
    param = "content="+encodeURIComponent(f.content.value)+"&latitude="+myMarker.getPosition().lat()+"&longitude="+myMarker.getPosition().lng();

    alert(param);
    //내마커 삭제
    URL = URL;
    xhr = getXMLHttpRequest();
    xhr.open("POST", URL, true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    xhr.onreadystatechange = responseHello;
    xhr.send(param);
}
 
function responseHello() {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var str = xhr.responseText;
            infowindow.setContent(str);
        } else {
            alert("Fail : " + xhr.status);
        }
    }
}

function noMyMarker()
{      
    var image = '/images/flight.png';
    
	
		var latlng = new google.maps.LatLng(map.getCenter().lat(),map.getCenter().lng());
	      	myMarker = new google.maps.Marker({
	          position: latlng,
	          draggable: true,
	          map: map,
	          title: '',
	          icon: image,
	          animation: google.maps.Animation.BOUNCE
	      });     

	    
	      google.maps.event.addListener(myMarker, 'click', function() {
	    	  
	          myMarker.setAnimation(google.maps.Animation.BOUNCE);
	      });

	      google.maps.event.addListener(myMarker, 'dragend', function() 
	 	  {
	 	    	  map.panTo(myMarker.getPosition());
	 	    	 myMarker.setAnimation(google.maps.Animation.BOUNCE);
	 	  });
    	
}


function setMyMarker(data)
{
	var image = '/images/flight.png';    
	var detail = data.split("\\nt");

  	var latlng = new google.maps.LatLng(detail[2],detail[3]);
  	myMarker = new google.maps.Marker({
      position: latlng,
      draggable: true,
      map: map,
      title: detail[4],
      icon: image,
      animation: google.maps.Animation.BOUNCE
    });
	var contentString = detail[1];

    infowindow = new google.maps.InfoWindow({
        content: contentString,
        disableAutoPan: true,
        maxWidth: 30
    });

    
     
      
      infowindow.open(map, myMarker);
      
      google.maps.event.addListener(myMarker, 'click', function() {
    	  
          infowindow.open(map, myMarker);
          myMarker.setAnimation(google.maps.Animation.BOUNCE);
      });

      google.maps.event.addListener(myMarker, 'dragend', function() 
 	  {
 	    	  map.panTo(myMarker.getPosition());
 	    	 myMarker.setAnimation(google.maps.Animation.BOUNCE);
 	  });
}



function putMarker(str)
{
    var data = str.split("\\rt");
   
    var image = '/images/flight.png';
    if (marker) 
    {     
    	for(i=0;i<marker.length;i++)
    		marker[i].setMap(null);
	} 
	marker = [];
    var idx = 0;
    for(i=0;i<data.length;i++)
    {          	
    	var detail = data[i].split("\\nt"); 
    	if(detail[0].indexOf("mymarker") >=0)
    	{
    		if(!myMarker )
        		setMyMarker(data[0]);
    	}
    	else
    	{
	    	var latlng = new google.maps.LatLng(detail[2],detail[3]);
	      	marker.push(new google.maps.Marker({
	          position: latlng,
	          draggable: false,
	          map: map,
	          title: detail[4],
	          icon: image,
	          animation: google.maps.Animation.DROP
	        }));
	      
	    	var contentString = detail[1];
	
		    infowindow = new google.maps.InfoWindow({
		        content: contentString,
		        disableAutoPan: true,
		        maxWidth: 30
		    });
				      
		    infowindow.open(map, marker[idx]);
		    idx ++;
    	}
    }

}
